﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibFriesland_Campina_CRM;

public partial class UserLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Lay thong tin dang nhap tu cookie
        HttpCookie fcUserCookie = Request.Cookies["fcUserCookie"];
        if (fcUserCookie != null)
        {
            login.UserName = fcUserCookie["username"];
            if (fcUserCookie["isRememberMeSet"] == "true")
                login.RememberMeSet = true;
            else
                login.RememberMeSet = false;
        }
    }

    protected void login_Authenticate(object sender, AuthenticateEventArgs e)
    {
        BizAccount account = BizAccount.Login(login.UserName, login.Password);
        if (account != null)
        {
            Session["account"] = account;
            e.Authenticated = true;
            //Luu thong tin dang nhap vao cookie
            if (login.RememberMeSet == true)
            {
                HttpCookie fcUserCookie = new HttpCookie("fcUserCookie");
                fcUserCookie["isRememberMeSet"] = "true";
                fcUserCookie["username"] = login.UserName;
                Response.Cookies.Add(fcUserCookie);
            }
            else
            {
                //Xoa thong tin dang nhap
                HttpCookie fcUserCookie = Request.Cookies["fcUserCookie"];
                if (fcUserCookie != null)
                {
                    fcUserCookie["username"] = "";
                    fcUserCookie["isRememberMeSet"] = "false";
                    Response.Cookies.Add(fcUserCookie);
                }
            }

            Response.Redirect("default.aspx");
        }
        else
            e.Authenticated = false;
    }

    protected void login_LoginError(object sender, EventArgs e)
    {
        login.FailureText = "Đăng nhập không thành công !";
    }
}